package com.stsdai.portal.controller;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.stsdai.cart.service.CartService;
import com.stsdai.manager.pojo.Cart;
import com.stsdai.manager.pojo.User;
import com.stsdai.portal.service.CartCookieService;
import com.stsdai.portal.util.CookieUtils;
import com.stsdai.sso.service.UserService;

@Controller
@RequestMapping("cart")
public class CartController {

	@Autowired
	private CartService cartService;

	@Autowired
	private CartCookieService cartCookieService;

	@Autowired
	private UserService userService;

	@Value("${STSD_TICKET}")
	private String STSD_TICKET;

	// http://www.taotao.com/cart/1159198734.html?num=3
	// 添加商品到购物车
	/**
	 * 添加商品到购物车
	 * 
	 * @param request
	 * @param itemId
	 * @param num
	 * @return
	 */
	@RequestMapping("{itemId}")
	public String addItemByCart(HttpServletRequest request, HttpServletResponse response, @PathVariable Long itemId,
			Integer num) {
		// 获取用户的登录信息
		// 从cookie中获取ticket
		String ticket = CookieUtils.getCookieValue(request, this.STSD_TICKET, true);
		// 使用单点登录服务，根据ticket查询用户登录信息
		User user = this.userService.queryUserByTicket(ticket);

		// 判断查询结果是否为非空
		if (user != null) {
			// 如果不为空表示用户已登录
			this.cartService.addItemByCart(user.getId(), itemId, num);
		} else {
			// 如果为空表示用户未登录
			this.cartCookieService.addItemByCart(request, response, itemId, num);
		}

		return "redirect:/cart/show.html";

	}

	// Controller返回值
	// 1. ModelAndView
	// 2. void
	// 使用response重定向
	// 使用request转发
	// 3. String
	// 返回逻辑视图名
	// 返回redirect关键词重定向，一个新的request和response，原来的参数丢了
	// 返回forward关键词转发，原来的request和response，原来的参数还在

	// http://www.taotao.com/cart/show.html
	/**
	 * 展示购物车详情
	 * 
	 * @param model
	 * @param request
	 * @return
	 */
	@RequestMapping("show")
	public String show(Model model, HttpServletRequest request) {
		// 获取用户登录信息
		String ticket = CookieUtils.getCookieValue(request, this.STSD_TICKET, true);
		User user = this.userService.queryUserByTicket(ticket);

		// 声明存放购物车的容器
		List<Cart> cartList = null;
		// 判断查询结果是否为非空
		if (user != null) {
			// 如果不为空表示用户已登录
			cartList = this.cartService.queryCartByUserId(user.getId());
		} else {
			// 如果为空表示用户未登录
			cartList = this.cartCookieService.queryCartByCookie(request);
		}

		// 把查询到的购物车放到Model ，传递给页面
		model.addAttribute("cartList", cartList);

		return "cart";
	}

	// http://www.taotao.com/service/cart/update/num/1299698061/123
	/**
	 * 修改购物车商品数量
	 * 
	 * @param request
	 * @param itemId
	 * @param num
	 */
	@RequestMapping(value = "update/num/{itemId}/{num}", method = RequestMethod.POST)
	@ResponseBody
	public void updateNumByCart(HttpServletRequest request, HttpServletResponse response, @PathVariable Long itemId,
			@PathVariable Integer num) {
		// 获取用户的登录信息
		// 从cookie中获取ticket
		String ticket = CookieUtils.getCookieValue(request, this.STSD_TICKET, true);
		// 使用单点登录服务，根据ticket查询用户登录信息
		User user = this.userService.queryUserByTicket(ticket);

		// 判断查询结果是否为非空
		if (user != null) {
			// 如果不为空表示用户已登录
			this.cartService.updateNumByCart(user.getId(), itemId, num);
		} else {
			// 如果为空表示用户未登录
			this.cartCookieService.updateNumByCart(request, response, itemId, num);
		}

	}

	// http://www.taotao.com/cart/delete/1299698061.html
	/**
	 * 删除购物车商品
	 * 
	 * @param request
	 * @param itemId
	 * @return
	 */
	@RequestMapping(value = "delete/{itemId}", method = RequestMethod.GET)
	public String deleteItemById(HttpServletRequest request, HttpServletResponse response, @PathVariable Long itemId) {
		// 获取用户的登录信息
		// 从cookie中获取ticket
		String ticket = CookieUtils.getCookieValue(request, this.STSD_TICKET, true);
		// 使用单点登录服务，根据ticket查询用户登录信息
		User user = this.userService.queryUserByTicket(ticket);

		// 判断查询结果是否为非空
		if (user != null) {
			// 如果不为空表示用户已登录
			this.cartService.deleteItemById(user.getId(), itemId);
		} else {
			// 如果为空表示用户未登录
			this.cartCookieService.deleteItemById(request, response, itemId);
		}

		return "redirect:/cart/show.html";
	}

}
